package org.dromara.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.dromara.business.domain.bo.MusicBo;
import org.dromara.business.domain.vo.MusicVo;
import org.dromara.business.domain.Music;
import org.dromara.business.mapper.MusicMapper;
import org.dromara.business.service.MusicService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 音乐Service业务层处理
 *
 * @author Lion Li
 * @date 2025-01-24
 */
@RequiredArgsConstructor
@Service
public class MusicServiceImpl extends ServiceImpl<MusicMapper,Music> implements MusicService {

    private final MusicMapper baseMapper;

    /**
     * 查询音乐
     */
    @Override
    public MusicVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询音乐列表
     */
    @Override
    public TableDataInfo<MusicVo> queryPageList(MusicBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<Music> lqw = buildQueryWrapper(bo);
        Page<MusicVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);

        return TableDataInfo.build(result);
    }

    /**
     * 查询音乐列表
     */
    @Override
    public List<MusicVo> queryList(MusicBo bo) {
        LambdaQueryWrapper<Music> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<Music> buildQueryWrapper(MusicBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Music> lqw = Wrappers.lambdaQuery();
        lqw.eq(StrUtil.isNotBlank(bo.getUrl()), Music::getUrl, bo.getUrl());
        lqw.orderByDesc(Music::getCreateTime);
        return lqw;
    }

    /**
     * 新增音乐
     */
    @Override
    public Boolean insertByBo(MusicBo bo) {
        Music add = MapstructUtils.convert(bo, Music.class);
        validEntityBeforeSave(add);
        String[] split = add.getUrl().split(",");
        boolean flag=false;
        for (String s : split) {
            add.setId(null);
            add.setUrl(s);
             flag = baseMapper.insert(add) > 0;
        }

        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改音乐
     */
    @Override
    public Boolean updateByBo(MusicBo bo) {
        Music update = MapstructUtils.convert(bo, Music.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(Music entity){

    }

    /**
     * 批量删除音乐
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public MusicVo getMusic() {
        QueryWrapper<Music> wrapper = new QueryWrapper<>();
        wrapper.last("ORDER BY RAND() LIMIT 1");
        Music music = baseMapper.selectOne(wrapper);
        return MapstructUtils.convert(music, MusicVo.class);
    }

}
